//
//  LDAboutSetViewController.m
//  Linkdood
//
//  Created by qirongkang on 16/4/27.
//  Copyright © 2016年 xiong qing. All rights reserved.
//

#import "LDAboutSetViewController.h"
#import "aboutAPPViewController.h"
#import "PersonalSettingViewController.h"
#import "contactViewController.h"
#import "SuggestController.h"
#import "ChatViewController.h"
@interface LDAboutSetViewController ()

@end

@implementation LDAboutSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"设置"];
    _table.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, _table.bounds.size.width, 0.01f)];
    _tipLab = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-40, self.view.center.y-20, 100, 40)];
    _tipLab.backgroundColor = [UIColor blackColor];
    _tipLab.textColor = [UIColor whiteColor];
    _tipLab.layer.cornerRadius = 10;
    _tipLab.layer.masksToBounds = YES;
    _tipLab.text = @"清除成功~";
    _tipLab.font = [UIFont systemFontOfSize:15];
    _tipLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_tipLab];
    _tipLab.hidden = YES;
    contentAry = [NSArray arrayWithObjects:@"个人设置",@"去评分",@"分享爱牙",@"意见反馈",@"加入爱牙QQ群",@"清理缓存",@"商务合作",@"关于爱牙", nil];
}

- (IBAction)backBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)clickMe:(UIButton *)sender {//夜间模式按钮
    sender.selected = !sender.selected;
    [[NSUserDefaults  standardUserDefaults] setObject:@(sender.selected) forKey:@"NightMode"];
    [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NightModeChanged" object:nil];
}

#pragma mark UITableViewDelegate & datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [contentAry count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identify = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        if (indexPath.row == 5) {
            _storage = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 35, 30)];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
            NSString *path = [paths lastObject];
            NSString *str = [NSString stringWithFormat:@"%.1fM", [self folderSizeAtPath:path]];
            _storage.text = str;
            _storage.font = [UIFont systemFontOfSize:13];
            _storage.textColor = [UIColor grayColor];
            cell.accessoryView = _storage;
        }else{
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14];
        [cell.textLabel setText:[contentAry objectAtIndex:indexPath.row]];

    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:{
            PersonalSettingViewController *personalSetVC = [[PersonalSettingViewController alloc] init];
            [self.navigationController pushViewController:personalSetVC animated:YES];
            break;
        }
        case 1:{
            break;
        }
        case 2:{
            break;
        }
        case 3:{//意见反馈
//            SuggestController *suggest = [[SuggestController alloc] init];
//            [self.navigationController pushViewController:suggest animated:YES];
            ChatViewController *chatVC= [[ChatViewController alloc] init];
            [self.navigationController pushViewController:chatVC animated:YES];
            break;
        }
        case 4:{
            break;
        }
        case 5:{
            UIAlertView *clearAlert = [[UIAlertView alloc] initWithTitle:@"清理缓存"
                                                                 message:@"你确定要删除所有缓存吗"
                                                                delegate:self
                                                       cancelButtonTitle:@"取消"
                                                       otherButtonTitles:@"确定", nil];
            [clearAlert show];
            break;
        }
        case 6:{
            contactViewController *bussinCooperateVC = [[contactViewController alloc] init];
            [self.navigationController pushViewController:bussinCooperateVC animated:YES];
            break;
        }
        case 7:{
            aboutAPPViewController *aboutAppVc = [[aboutAPPViewController alloc] init];
            [self.navigationController pushViewController:aboutAppVc animated:YES];
            break;
        }
        default:
            break;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *path = [paths lastObject];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
        for (NSString *p in files) {
            NSError *error;
            NSString *Path = [path stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
                [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
            }
        }
        [self.view makeToast:@"清除成功!" duration:2.0 position:@"center"];
        NSString *str = [NSString stringWithFormat:@"%.1fM", [self folderSizeAtPath:path]];
        _storage.text = str;
    }
}

- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

//遍历文件夹获得文件夹大小，返回多少M

- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
    
}

@end





